package edu.zhku.ttms.service.impl;

import edu.zhku.ttms.domain.User;
import edu.zhku.ttms.mapper.TrafficPolicemanMapper;
import edu.zhku.ttms.mapper.UserMapper;
import edu.zhku.ttms.service.UserService;
import edu.zhku.ttms.utils.LoginSession;
import edu.zhku.ttms.utils.MybatisUtil;
import edu.zhku.ttms.utils.StringUtil;
import edu.zhku.ttms.utils.SystemConst;

import java.util.regex.Pattern;

/**
 * @author Feng
 * @since 2021/4/19 12:49
 */

public class UserServiceImpl implements UserService {

    static UserMapper userMapper = MybatisUtil.getMapper(UserMapper.class);

    static TrafficPolicemanMapper trafficPolicemanMapper = MybatisUtil.getMapper(TrafficPolicemanMapper.class);

    @Override
    public String register(String loginName, String password, String rePassword, Integer roleId) {
        if (roleId == null) {
            return "须选择身份！";
        }
        if (StringUtil.isEmpty(loginName)) {
            return "用户名不能为空！";
        }
        if (StringUtil.isEmpty(password)) {
            return "密码不能为空！";
        }
        if (!password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$")) {
            return "密码至少包含数字和英文，长度6-20";
        }
        if (!password.equals(rePassword)) {
            return "两次密码须相同！";
        }
        if (roleId == 3 && !loginName.matches("^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$")) {
            return "驾驶证号格式错误！";
        }
        if (roleId == 2 && trafficPolicemanMapper.getByCode(loginName) == null) {
            return "未录入该交警代号";
        }
        if (userMapper.getByLoginName(loginName) != null) {
            return "该用户已存在！";
        }
        userMapper.insertOne(new User(loginName, password, roleId));
        return SystemConst.SUCCESS;
    }

    @Override
    public String modifyPassword(String originalPwd, String newPwd, String reNewPwd) {
        if (StringUtil.isEmpty(originalPwd)) {
            return "请输入原密码";
        }
        if (StringUtil.isEmpty(newPwd)) {
            return "新密码不能为空";
        }
        if (!newPwd.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$")) {
            return "密码至少包含数字和英文，长度6-20";
        }
        if (!newPwd.equals(reNewPwd)) {
            return "两次输入的新密码不同";
        }
        String loggerUserName = LoginSession.getLoggedUserName();
        User loggedUser = userMapper.getByLoginName(loggerUserName);
        if (!originalPwd.equals(loggedUser.getPassword())) {
            return "原密码错误！";
        }
        loggedUser.setPassword(newPwd);
        userMapper.updateOne(loggedUser);
        logout();
        return SystemConst.SUCCESS;
    }

    @Override
    public String login(String loginName, String password) {
        if (StringUtil.isEmpty(loginName)) {
            return "请输入用户名！";
        }
        if (StringUtil.isEmpty(password)) {
            return "请输入密码！";
        }
        User user = userMapper.getByLoginName(loginName);
        if (user == null || !password.equals(user.getPassword())) {
            return "用户名或密码输入错误！";
        }
        LoginSession.login(user);
        return SystemConst.SUCCESS;
    }

    @Override
    public void logout() {
        LoginSession.logout();
    }
}
